<template>
  <AssetAssignTree ref="refAssignTree" :comment="props.comment"
                   :name="'菜单'" :blank-tip="blankTip" :load-asset-data="loadMenuData"
                   :load-hold-asset="loadHoldAsset" :assign-assets="assignMenu"
                   :product="props.product" :path="'/platform/assets/menus/'"/>
</template>

<script setup lang="ts">

import {ref} from "vue";
import AssetAssignTree from "@/widget/assign/AssetAssignTree.vue";
import {useLoadAssign} from "@/utils/UseLoadAssign";
import {loadAssetLevel} from "@/apis";
import {TYPES} from "@/stores/conf";
import {useStore} from "@/stores";
import {handleLevelMenus} from "@/utils/SystemUtil";

const store = useStore();
const refAssignTree = ref();

const props = withDefaults(defineProps<{
  entry: any,
  comment?: string,
  product?: boolean,
}>(), {});

const blankTip = "当前平台还未分配任何菜单,请点击下方按钮分配菜单.";

const {load, assign} = useLoadAssign(props.entry);

const getTargetId = () => {
  return '';
}

const productCode = () => {
  return refAssignTree?.value.getProductCode();
}

const refresh = async () => {
  await refAssignTree.value?.refresh();
}

const loadMenuData = async (params: any) => {
  const platform: any = store.getCurrentPlatform();
  const result = await loadAssetLevel({
    server: props.entry.server,
    targetId: platform.id,
    assetType: TYPES.MENU,
    ...params,
    productCode: productCode(),
  });

  if (result && result.data) {
    const menuData: any = [];
    handleLevelMenus(menuData, result.data, []);
    delete result.data;
    result.data = menuData;
  }

  return result;
}

const loadHoldAsset = async () => {
  return await load(getTargetId(), TYPES.MENU, {
    current: 1,
    pageSize: 9999,
    productCode: productCode(),
  });
}

const assignMenu = async (assets: any) => {
  return await assign(getTargetId(), TYPES.MENU, assets, productCode(), 1);
}

defineExpose({
  refresh,
})

</script>

<style scoped>
.el-form-item-class .el-form-item__label {
  font-size: 12px;
}

</style>
